package com.dstation.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dstation.dao.MessageMapper;
import com.dstation.domain.*;
import com.dstation.service.interf.IMessageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class MessageServiceImpl implements IMessageService {

    @Resource
    private MessageMapper messageMapper;



    @Override
    public IPage<?> findCommentMessage(Long userId, Page page) {
        IPage<?> videoCommentInfoIPage = messageMapper.selectCommentMessage(userId, page);
        List<?> records = videoCommentInfoIPage.getRecords();

        List<CommonMessage> messages=new ArrayList<>();
        for(int i=0;i<records.size();i++){
            messages.add(new CommonMessage((VideoCommentInfo) records.get(i)));
        }

        IPage<CommonMessage> ipage=formatPage(videoCommentInfoIPage);

        ipage.setRecords(messages);

        return ipage;

    }



    @Override
    public IPage<?> findVideoMessage(Long userId,Page page) {
        IPage<?> videoInfoIPage = messageMapper.selectVideoMessage(userId, page);
        List<?> records = videoInfoIPage.getRecords();

        List<CommonMessage> messages=new ArrayList<>();
        for(int i=0;i<records.size();i++){
            messages.add(new CommonMessage((VideoInfo) records.get(i)));
        }

        IPage<CommonMessage> ipage=formatPage(videoInfoIPage);



        ipage.setRecords(messages);

        return ipage;
    }



    @Override
    public IPage<?> findDynamicMessage(Long userId,Page page) {
        IPage<?> dataPage=messageMapper.selectDynamicMessage(userId,page);

        List<?> records = dataPage.getRecords();

        List<CommonMessage> messages=new ArrayList<>();

        for(int i=0;i<records.size();i++){
            messages.add(new CommonMessage((DynamicInfo) records.get(i)));
        }

        IPage<CommonMessage> ipage=formatPage(dataPage);

        ipage.setRecords(messages);

        return ipage;
    }

    @Override
    public IPage<?> findLikedMessage(Long userId,Page page) {
        IPage<?> dataPage= messageMapper.selectLikedMessage(userId,page);

        List<?> records = dataPage.getRecords();

        List<CommonMessage> messages=new ArrayList<>();

        for(int i=0;i<records.size();i++){
            messages.add(new CommonMessage((VideoLikedInfo) records.get(i)));
        }

        IPage<CommonMessage> ipage=formatPage(dataPage);

        ipage.setRecords(messages);

        return ipage;
    }

    @Override
    public IPage<?> findCoinMessage(Long userId,Page page) {

        IPage<CoinOperatedInfo> dataPage = messageMapper.selectCoinMessage(userId, page);

        List<?> records = dataPage.getRecords();

        List<CommonMessage> messages=new ArrayList<>();

        for(int i=0;i<records.size();i++){
            messages.add(new CommonMessage((CoinOperatedInfo) records.get(i)));
        }

        IPage<CommonMessage> ipage=formatPage(dataPage);

        ipage.setRecords(messages);

        return ipage;
    }

    @Override
    public IPage<?> findRechargeMessage(Long userId,Page page) {

        IPage<OrderInfo> dataPage = messageMapper.selectRechargeMessage(userId, page);

        List<?> records = dataPage.getRecords();

        List<CommonMessage> messages=new ArrayList<>();

        for(int i=0;i<records.size();i++){
            messages.add(new CommonMessage((OrderInfo) records.get(i)));
        }

        IPage<CommonMessage> ipage=formatPage(dataPage);

        ipage.setRecords(messages);

        return ipage;
    }

    private IPage<CommonMessage> formatPage(IPage<?> videoCommentInfoIPage) {
        IPage<CommonMessage> result=new Page<>();
        result.setTotal(videoCommentInfoIPage.getTotal());
        result.setCurrent(videoCommentInfoIPage.getCurrent());
        result.setSize(videoCommentInfoIPage.getSize());




        return result;
    }
}
